<!--
 * @Author: 千锋爱佛僧
 * @公众号: 大前端私房菜
 * @Slogan: 千锋精品教程，好学得不像实力派！
-->
<template>
  <div class="box">
    <h1>A组件,{{ count }}</h1>
    <el-button type="primary" @click="handlePlus">数字+</el-button>
    <el-button type="primary" @click="handleAdd">新增商品</el-button>
    <div v-for="(item, index) in list" :key="index">
      商品名称:{{ item.name }} 商品价格：{{ item.price }}
    </div>
  </div>
</template>

<script>
export default {
  computed: {
    //状态机数据必须在computed中提取
    count() {
      return this.$store.state.count.num;
    },
    list() {
      return this.$store.state.goods.goodsList;
    },
  },
  methods: {
    handlePlus() {
      this.$store.commit("count/increment", 10); //触发mutations的执行
    },
    handleAdd() {
      this.$store.commit("goods/addGoods", {
        name: "新商品名称",
        price: Math.round(Math.random() * 400 + 200),
        num: 1,
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.box {
  height: 500px;
  width: 50%;
  border: 1px solid #333;
}
</style>
